 /**
 * @(#)client.java
 *
 *
 * @author 
 * @version 1.00 2014/2/28
 */
import java.io.*;
import java.net.*;
import java.util.*;   
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class client {
        
    private InetAddress address;
    private Socket socket=null;
    private String message=null;
    private JOptionPane optionpane;
    private PrintWriter lineout;
    private JTextArea textarea;
    private JFrame frame;
    private JTextField textfield;
    private String name="Client";
    public client() {
    	
    	System.out.println("running client..");
    	name = optionpane.showInputDialog(new JFrame(),"Enter your name");
    	message = optionpane.showInputDialog(new JFrame(),"Enter the servers local address");
    	connect();
    	
    }
    public void connect(){
    
    	try{
    		socket = new Socket(message,3333);
    		 BufferedReader inline = 
    			new BufferedReader(new InputStreamReader(socket.getInputStream()));
    			lineout = new PrintWriter(socket.getOutputStream(), true);
    			showGui();
    			textfieldListener();
    		while((message=inline.readLine())!=null){
    			textarea.append(message+"\n");
    		}
    	}catch (Exception e){
    		System.out.println(e);
    	}
    	
    }
    public void showGui(){
    		textarea = new JTextArea();
    		textfield = new JTextField();
    		frame = new JFrame("Client");
    		frame.add(new JScrollPane(textarea));
    		frame.add(textfield,"South");
    		frame.setSize(300,390);
    		frame.setVisible(true);
    }
    
    
    public void textfieldListener(){
    		textfield.addActionListener(new ActionListener() {
      		public void actionPerformed(ActionEvent event) {
        		sendMessage(textfield.getText());
        		textfield.setText("");
     			 }
    		});
    }
    public void sendMessage(String m){
    	lineout.println(name+":"+m);
    }
    public static void main(String[] args) {
        new client();
    }
}
